package com.nowcoder.community;

import com.nowcoder.community.util.MailClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)  //引用应用配置类
public class MailTEst {

    @Autowired
    private MailClient mailClient;

    /* 注意：在Controller中，只需要返回模版引擎的路径，dispathcerSerclet会帮我们自动调用，而在测试类中，必须主动注入模版引擎对象进行调用，*/
    @Autowired
    private TemplateEngine templateEngine;

    //发送普通文本测试方法
    @Test
    public void testTextMail(){
        mailClient.sendMail("2642309430@qq.com","Test","welcome");
    }

    //发送html邮件测试方法
    @Test
    public void testHtmlMail(){
        //访问模版，需要传一个参数，构造参数
        Context context = new Context();
        //把传入模版的变量传入这个对象
        context.setVariable("username","sunday");

        //调用模版引擎生成html：传入模版文件路径和内容
        String content = templateEngine.process("/mail/demo",context);
        System.out.println(content);

        //使用邮件发送给浏览器，浏览器自动渲染
        mailClient.sendMail("2642309430@qq.com","Test",content);

    }


}
